"use strict";
var Premium = Premium || {};


Premium.creative = {
    // vastTag: "https://track.adform.net/serving/videoad/?bn=22592277",
    vastTag: { src: "https://video-content.gumgum.com/edr-677-emea-adidas-ss23running-fr_out/7320438595509082275/edr-677-emea-adidas-ss23running-fr_out_2.mp4", poster: 'assets/poster.jpg', type: 'video/mp4' },
    init: function() {
        /* START OF CUSTOM JS */

        Premium.communicator.init(document.body.id === "body_expanded" ? undefined : 4);

        switch (document.body.id) {
            case "body_top":
                // TOP ---------------------------------------------------------------

                var videoContainer = document.querySelector("#video-container");
                var videoEl;

                var playerReadyHandler = function() {
                    Premium.audio.inactiveTabListener();
                    videoEl = videoContainer.querySelector("VIDEO");
                    Premium.video.switchOnScroll(videoContainer, 2);
                    Premium.video.sync(videoEl, 1);
                    var prom = videoEl.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                Premium.jpxApi.createVideo('top', videoContainer, Premium.creative.vastTag, 'showfullscreen');
                Premium.jpxApi.videoEvent('playerAppended', 'top', playerReadyHandler);

                PremiumJpControls.callOnClickFullScreen(function(el) {
                    videoEl.muted = false;
                    Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                })
                break;

            case "body_left":
          
                break;

            case "body_right":
                // RIGHT ---------------------------------------------------------------
                var videoContainer = document.querySelector("#video-container");
                var videoEl;

                var playerReadyHandler = function() {
                    Premium.audio.inactiveTabListener();
                    videoEl = videoContainer.querySelector("VIDEO");
                    Premium.video.switchOnScroll(videoContainer, 2);
                    Premium.video.sync(videoEl, 1);
                    videoEl.volume = 0;
                    var prom = videoEl.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }  
                }

                Premium.jpxApi.createVideo('right', videoContainer, Premium.creative.vastTag, 'showfullscreen');
                Premium.jpxApi.videoEvent('playerAppended', 'right', playerReadyHandler);

                PremiumJpControls.callOnClickFullScreen(function(el) {
                    videoEl.muted = false;
                    Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                })
                break;

            case "body_expanded":

                break;

        }
        /* END OF CUSTOM JS */
    },
    loaded: function() {
        if (document.body.id === "body_expanded") {
            var videoContainer = document.querySelector("#video-container");

            var playerReadyHandler = function() {
                Premium.audio.inactiveTabListener();
                var videoEl = videoContainer.querySelector("VIDEO");
                videoEl.volume = 0;
                videoEl.addEventListener("playing", function() {
                    setTimeout(function() {
                        document.body.className += " fade-in";
                    }, 100);
                }, { once: true })
                Premium.video.sync(videoEl, undefined, Premium.video.SyncType_Get);
                var prom = videoEl.play(); // Explicitly play the video
                if (prom) {
                    prom.catch(function() {})
                }

                // Safari fix
                document.body.addEventListener("click", function(e) {
                    if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
                        if (!videoEl.paused) {
                            setTimeout(function() {
                                videoEl.play();
                            }, 1000)

                        }
                    }
                })
            }

            Premium.jpxApi.createVideo('expanded', videoContainer, Premium.creative.vastTag);
            Premium.jpxApi.videoEvent('playerAppended', 'expanded', playerReadyHandler);

        }
    }
}